﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    public sealed partial class GameOverState : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        Menu menu;

        public static int nrPat;
        public static int nrPatsaved;
        public static int nrOfFires;
        public static int totalTime;
        public static int totalScore;

        public static void clearStats()
        {
            nrPat = 0;
            nrPatsaved = 0;
            nrOfFires = 0;
            totalTime = 0;
            totalScore = 0;
        }

        public static void setStats(int nrPate, int nrPatesaved, int nrOfFiress, int totalTimes)
        {
            nrPat = nrPate;
            nrPatsaved = nrPatesaved;
            nrOfFires = nrOfFiress;
            totalTime = totalTimes;
            totalScore = Math.Max(0, 1000 - totalTime + nrPat - nrPatsaved + nrOfFires); //??????
        }

        private void buttonMainMenu()
        {
            Game.Components.RemoveAt(Game.Components.Count - 1);
            StateManager.currentState = States.MAINMENU;
            Game.Components.Add(new PlayGameState(Game, spriteBatch));
            if (GameVariables.music)
            {
                MediaPlayer.Stop();
                MediaPlayer.Play(ResourceManager.getSong("menuSong"));
            }
        }

        public GameOverState(Game game, SpriteBatch spriteBatch)
            : base(game)
        {
            this.spriteBatch = spriteBatch;

            ButtonContainer buttons = new ButtonContainer();

            buttons.AddButton(ResourceManager.getTexture("buttonMainMenu"),
                                new ButtonEvent(buttonMainMenu),
                                Color.White,
                                Color.Orange,
                                Color.Red);

            menu = new Menu(game, buttons, 297, 52, (int)(game.Window.ClientBounds.Height*0.8f));
        }

        public sealed override void Update(GameTime gameTime)
        {
            if (StateManager.currentState == States.GAMEOVER)
            {
                menu.Update(gameTime);
            }
        }

        public sealed override void Draw(GameTime gameTime)
        {
            if (StateManager.currentState == States.GAMEOVER)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(ResourceManager.getTexture("Loading"), Game.GraphicsDevice.Viewport.TitleSafeArea, Color.White);
                spriteBatch.DrawString(ResourceManager.getFont("JingJingSmall"), "Number of Patients:             " + nrPat.ToString(), new Vector2(100, (float)(Game.GraphicsDevice.Viewport.Height * 0.5)), Color.Black);
                spriteBatch.DrawString(ResourceManager.getFont("JingJingSmall"), "Number of Patients saved:  " + nrPatsaved.ToString(), new Vector2(100, (float)(Game.GraphicsDevice.Viewport.Height * 0.55)), Color.Black);
                spriteBatch.DrawString(ResourceManager.getFont("JingJingSmall"), "Number of Fires:                    " + nrOfFires.ToString(), new Vector2(100, (float)(Game.GraphicsDevice.Viewport.Height * 0.6)), Color.Black);
                spriteBatch.DrawString(ResourceManager.getFont("JingJingSmall"), "Total time:                               " + totalTime.ToString(), new Vector2(100, (float)(Game.GraphicsDevice.Viewport.Height * 0.65)), Color.Black);
                spriteBatch.DrawString(ResourceManager.getFont("JingJingSmall"), "Total score:                             " + totalScore.ToString(), new Vector2(100, (float)(Game.GraphicsDevice.Viewport.Height * 0.7)), Color.Green);
                
                spriteBatch.DrawString(ResourceManager.getFont("JingJing"), "Game Over", new Vector2(50, Game.GraphicsDevice.Viewport.Height - 120), Color.Orange);
                spriteBatch.End();
                menu.Draw(spriteBatch);
            }
        }
    }
}